<?php
/**
 * Author: lee wei
 * Date: 2021/12/8
 */
namespace app\common;

use think\facade\Env;
use think\Response;

trait JsonResponse
{

    private $debug = [];

    /**
     * 成功的返回
     */
    public function buildSuccess(array $data = [], array $response = Coderesponse::SUCCESS): Response
    {
        list($code,$msg) = $response;

        $return = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        if (Env::get('APP_DEBUG') && $this->debug) {
            $return['debug'] = $this->debug;
        }

        return json($return);
    }

    /**
     * 错误的返回
     * @return Response
     */
    public function buildFailed(array $response = Coderesponse::FAIL, string $info = '', array $data = []): Response {

        list($code,$msg) = $response;
        $msg = $info ?: $msg;

        $return = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        if (Env::get('APP_DEBUG') /*&& $this->debug*/) {
            $return['debug'] = $this->debug;
        }

/*        if ($this->app->isDebug()) {
            // 调试模式，获取详细的错误信息
            $traces        = [];
            $nextException = $exception;
            do {
                $traces[] = [
                    'name'    => get_class($nextException),
                    'file'    => $nextException->getFile(),
                    'line'    => $nextException->getLine(),
                    'code'    => $this->getCode($nextException),
                    'message' => $this->getMessage($nextException),
                    'trace'   => $nextException->getTrace(),
                    'source'  => $this->getSourceCode($nextException),
                ];
            } while ($nextException = $nextException->getPrevious());
            $data = [
                'code'    => $this->getCode($exception),
                'message' => $this->getMessage($exception),
                'traces'  => $traces,
                'datas'   => $this->getExtendData($exception),
                'tables'  => [
                    'GET Data'            => $this->app->request->get(),
                    'POST Data'           => $this->app->request->post(),
                    'Files'               => $this->app->request->file(),
                    'Cookies'             => $this->app->request->cookie(),
                    'Session'             => $this->app->exists('session') ? $this->app->session->all() : [],
                    'Server/Request Data' => $this->app->request->server(),
                ],
            ];
        }*/

        return json($return);
    }

    /**
     * debug参数收集
     * @param $data
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    protected function debug($data): void {
        if ($data) {
            $this->debug[] = $data;
        }
    }

    /**
     * 返回封装后的API数据到客户端
     * @param  mixed   $data 要返回的数据
     * @param  integer $code 返回的code
     * @param  mixed   $message 提示信息
     * @param  string  $type 返回数据格式
     * @param  array   $header 发送的Header信息
     * @return Response
     */
    public function Success($data , string $message = '请求成功', int $code = 200, string $type = 'json',$header = []) :Response
    {
        $result = [
            'code' => $code,
            'message' => $message,
            'time' => time(),
            'data' => $data
        ];
        return Response::create($result,$type)->header($header);

    }

    /**
     * 返回封装后的API数据到客户端
     * @param  mixed   $data 要返回的数据
     * @param  integer $code 返回的code
     * @param  mixed   $message 提示信息
     * @param  string  $type 返回数据格式
     * @param  array   $header 发送的Header信息
     * @return Response
     */
    public function Error($data , string $message = '请求失败', int $code = 200, string $type = 'json',$header = []) :Response
    {
        $result = [
            'code' => $code,
            'message' => $message,
            'time' => time(),
            'data' => $data
        ];
        return Response::create($result,$type)->header($header);
    }
}
